{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-10T08:28:24.203977Z",
     "start_time": "2025-04-10T08:28:21.161543Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display, clear_output"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-10T10:03:29.680231Z",
     "start_time": "2025-04-10T10:03:29.589476Z"
    }
   },
   "cell_type": "code",
   "source": [
    "file_path = \"02500005_5min.csv\"\n",
    "# 读取原始数据\n",
    "raw_data = pd.read_csv(file_path)\n",
    "\n",
    "print(len(raw_data))\n",
    "\n",
    "raw_data['data_time'] = pd.to_datetime(raw_data['data_time'], format='%Y-%m-%d %H:%M:%S')\n",
    "\n",
    "# 提取月份和小时值，并创建新列 h 和 m\n",
    "raw_data['h'] = raw_data['data_time'].dt.hour\n",
    "raw_data['m'] = raw_data['data_time'].dt.month\n",
    "raw_data[\"mm\"] = raw_data['data_time'].dt.minute\n",
    "\n",
    "data_time = raw_data['data_time'].values"
   ],
   "id": "c24c94927466dde1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21600\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-10T10:03:30.594637Z",
     "start_time": "2025-04-10T10:03:30.302883Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取除 data_time 以外的所有列名\n",
    "columns_to_plot = [col for col in raw_data.columns if col != 'data_time']\n",
    "\n",
    "# 获取时间范围\n",
    "min_time = data_time.min()\n",
    "max_time = data_time.max()\n",
    "\n",
    "# 为每个列生成一个按钮\n",
    "show_columns = {col: widgets.ToggleButton(\n",
    "    value=True,\n",
    "    description=f'Show {col}',\n",
    "    disabled=False,\n",
    "    button_style='',\n",
    "    tooltip=f'Toggle {col}',\n",
    "    icon='check'\n",
    ") for col in columns_to_plot}\n",
    "\n",
    "# 初始化时间滑动条\n",
    "start_time_slider = widgets.SelectionSlider(\n",
    "    options=data_time,\n",
    "    value=min_time,\n",
    "    description='Start Time:',\n",
    "    continuous_update=False,\n",
    "    layout=widgets.Layout(width='800px')  # 设置滑动条宽度为800像素\n",
    ")\n",
    "end_time_slider = widgets.SelectionSlider(\n",
    "    options=data_time,\n",
    "    value=max_time,\n",
    "    description='End Time:',\n",
    "    continuous_update=False,\n",
    "    layout=widgets.Layout(width='800px')  # 设置滑动条宽度为800像素\n",
    ")\n",
    "\n",
    "# 初始化 y 轴范围滑动条\n",
    "y_bottom_slider = widgets.IntSlider(\n",
    "    value=0,\n",
    "    min=-1000,\n",
    "    max=1000,\n",
    "    step=10,\n",
    "    description='Y Bottom:',\n",
    "    continuous_update=False,\n",
    "    layout=widgets.Layout(width='800px')\n",
    ")\n",
    "\n",
    "y_top_slider = widgets.IntSlider(\n",
    "    value=1000,\n",
    "    min=-1000,\n",
    "    max=1000,\n",
    "    step=5,\n",
    "    description='Y Top:',\n",
    "    continuous_update=False,\n",
    "    layout=widgets.Layout(width='800px')\n",
    ")\n"
   ],
   "id": "40c0a379ab5686ef",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-10T10:03:32.361804Z",
     "start_time": "2025-04-10T10:03:31.028022Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 更新图形的函数\n",
    "def update_plot(start_time, end_time, y_bottom, y_top , **show_columns ):\n",
    "    # 清除之前的输出\n",
    "    clear_output(wait=True)\n",
    "\n",
    "    # 创建图形对象\n",
    "    fig, ax = plt.subplots(figsize=(18, 9))\n",
    "\n",
    "    # 获取时间范围内的索引\n",
    "    start_index = raw_data[raw_data['data_time'] == start_time].index[0]\n",
    "    end_index = raw_data[raw_data['data_time'] == end_time].index[0]\n",
    "\n",
    "     # 遍历所有需要绘制的列\n",
    "    for col, show in show_columns.items():\n",
    "        if show:  # 只绘制用户选择显示的列\n",
    "            ax.plot(data_time[start_index:end_index], raw_data[col][start_index:end_index], label=col, marker='.', linestyle='-')\n",
    "\n",
    "\n",
    "    # 添加图例\n",
    "    ax.legend()\n",
    "\n",
    "    # 添加标题和坐标轴标签\n",
    "    ax.set_title('All Columns over Time')\n",
    "    ax.set_xlabel('Time')\n",
    "    ax.set_ylabel('Value')\n",
    "\n",
    "    # 设置时间轴的格式\n",
    "    fig.autofmt_xdate()  # 自动旋转日期标签，避免重叠\n",
    "    ax.grid(True)  # 添加网格线\n",
    "\n",
    "    # 限制 y 轴的最大值\n",
    "    ax.set_ylim(bottom=y_bottom, top=y_top)  # 设置 y 轴的范围，可以根据需要调整 top 的值\n",
    "\n",
    "    # 显示图形\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# 将时间滑动条的值绑定到更新函数\n",
    "widgets.interactive(update_plot, start_time=start_time_slider, end_time=end_time_slider, y_bottom=y_bottom_slider, y_top=y_top_slider, **show_columns)\n"
   ],
   "id": "dac0cd8466595216",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "interactive(children=(SelectionSlider(continuous_update=False, description='Start Time:', layout=Layout(width=…"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "db63ff2adf5f42b1845eb68d4433d6f8"
      }
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-10T08:29:31.930213Z",
     "start_time": "2025-04-10T08:29:31.925226Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "ce227cdbc16b0a7f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "dd58bf05cdf9e1bc"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
